#include "menubar.h"
#include "mainwindow.h"

MenuBar::MenuBar(TextEditorsManager *editorManager, MainWindow *mainWindow) :
    QMenuBar(mainWindow)
{
    textEditorsManager_ = editorManager;

    QMenu * menu;
    QAction * action;

    /* -------------------
    ||     File menu    ||
    ----------------------*/

    menu = new QMenu("&File", this);

    action = new QAction("&New File", menu);
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(newFile()));
    action->setShortcut(QKeySequence::New);
    menu->addAction(action);

    action = new QAction("&Open File", menu);
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(openFile()));
    action->setShortcut(QKeySequence::Open);
    menu->addAction(action);

    menu->addSeparator();

    action = new QAction("&Save File", menu);
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(saveFile()));
    action->setShortcut(QKeySequence::Save);
    menu->addAction(action);

    action = new QAction("Save &As...", menu);
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(saveAs()));
    menu->addAction(action);

    action =  new QAction("Save A&ll", menu);
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(saveAll()));
    action->setShortcut(QKeySequence("Ctrl+Shift+S"));
    menu->addAction(action);

    menu->addSeparator();

    action = new QAction("E&xit", menu);
    connect(action, SIGNAL(triggered()), mainWindow, SLOT(close()));
    action->setShortcut(QKeySequence::Quit);
    menu->addAction(action);

    addMenu(menu);


    /* -------------------
    ||     Edit menu    ||
    ----------------------*/

    menu = new QMenu("&Edit", this);

    action = new QAction("&Undo", menu);
    connect(textEditorsManager_, SIGNAL(undoAvailable(bool)), action, SLOT(setEnabled(bool)));
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(undo()));
    action->setShortcut(QKeySequence::Undo);
    menu->addAction(action);

    action = new QAction("&Redo", menu);
    connect(textEditorsManager_, SIGNAL(redoAvailable(bool)), action, SLOT(setEnabled(bool)));
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(redo()));
    action->setShortcut(QKeySequence::Redo);
    menu->addAction(action);

    menu->addSeparator();

    action = new QAction("Cu&t", menu);
    connect(textEditorsManager_, SIGNAL(copyAvailable(bool)), action, SLOT(setEnabled(bool)));
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(cut()));
    action->setShortcut(QKeySequence::Cut);
    menu->addAction(action);

    action = new QAction("&Copy", menu);
    connect(textEditorsManager_, SIGNAL(copyAvailable(bool)), action, SLOT(setEnabled(bool)));
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(copy()));
    action->setShortcut(QKeySequence::Copy);
    menu->addAction(action);

    action = new QAction("&Paste", menu);
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(paste()));
    action->setShortcut(QKeySequence::Paste);
    menu->addAction(action);

    menu->addSeparator();

    action = new QAction("Select &All", menu);
    connect(action, SIGNAL(triggered()), textEditorsManager_, SLOT(selectAll()));
    action->setShortcut(QKeySequence::SelectAll);
    menu->addAction(action);

    menu->addSeparator();

    action = new QAction("Pre&ferences...", menu);
    //connect(action, SIGNAL(triggered()), settingsManager_, SLOT(open()));
    action->setShortcut(QKeySequence::Preferences);
    menu->addAction(action);

    addMenu(menu);
}
